<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:videoInfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:videoInfo:remove']"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="videoInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序列号" align="center" prop="seqCode"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="ip地址" align="center" prop="ipCode"/>
      <el-table-column label="端口号" align="center" prop="portCode"/>
      <el-table-column label="内网ip" align="center" prop="insideIp"/>
      <el-table-column label="内网端口" align="center" prop="insidePort"/>
      <el-table-column label="手机端口" align="center" prop="telPortCode"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:videoInfo:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:videoInfo:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div class="el-dialog-div">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="序列号" prop="seqCode">
                <el-input v-model="form.seqCode" placeholder="请输入序列号"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="ip地址" prop="ipCode">
                <el-input v-model="form.ipCode" placeholder="请输入ip地址"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="端口号" prop="portCode">
                <el-input v-model="form.portCode" placeholder="请输入端口号"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内网ip" prop="insideIp">
                <el-input v-model="form.insideIp" placeholder="请输入内网ip"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内网端口" prop="insidePort">
                <el-input v-model="form.insidePort" placeholder="请输入内网端口"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否云台" prop="isYt">
                <el-select v-model="form.isYt" placeholder="请选择主页显示">
                  <el-option
                    v-for="dict in isYtOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="手机端口" prop="telPortCode">
                <el-input v-model="form.telPortCode" placeholder="请输入手机端口"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="账号" prop="account">
                <el-input v-model="form.account" placeholder="请输入账号"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" placeholder="请输入密码"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="form.sort" placeholder="请输入排序" :precision="0" :min="1" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listVideoInfo,
    getVideoInfo,
    delVideoInfo,
    addVideoInfo,
    updateVideoInfo,
    exportVideoInfo
  } from "@/api/basic/videoInfo";

  export default {
    name: "VideoInfo",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 视频配置表格数据
        videoInfoList: [],
        isYtOptions: [{dictLabel:'是',dictValue:'Y'},{dictLabel:'否',dictValue:'N'}],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          seqCode: undefined,
          name: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          seqCode: [
            { required: true, message: "序列号不能为空", trigger: "blur" }
          ],
          ipCode: [
            { required: true, message: "ip地址不能为空", trigger: "blur" }
          ],
          portCode: [
            { required: true, message: "端口号不能为空", trigger: "blur" }
          ],
          insideIp: [
            { required: true, message: "内网ip不能为空", trigger: "blur" }
          ],
          insidePort: [
            { required: true, message: "内网端口不能为空", trigger: "blur" }
          ],
          isYt: [
            { required: true, message: "是否云台不能为空", trigger: "blur" }
          ],
          telPortCode: [
            { required: true, message: "手机端口不能为空", trigger: "blur" }
          ],
          account: [
            { required: true, message: "账号不能为空", trigger: "blur" }
          ],
          password: [
            { required: true, message: "密码不能为空", trigger: "blur" }
          ],
          sort: [
            { required: true, message: "排序不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询视频配置列表 */
      getList() {
        this.loading = true;
        listVideoInfo(this.queryParams).then(response => {
          this.videoInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          seqCode: undefined,
          ipCode: undefined,
          portCode: undefined,
          account: undefined,
          password: undefined,
          telPortCode: undefined,
          sort: undefined,
          name: undefined,
          insideIp: undefined,
          insidePort: undefined,
          isYt:undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加视频配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getVideoInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改视频配置";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateVideoInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addVideoInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除选中的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delVideoInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有视频配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportVideoInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
